package com.hurt.competition.admin.controller;

import com.hurt.competition.admin.service.EmailService;
import com.hurt.competition.common.data.ResetPwdCodeData;
import com.hurt.competition.common.util.CoreResultBuilder;
import com.hurt.competition.common.vo.CoreResult;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("email")
public class EmailController {

    // 邮件主题
    private static final String subject = "忘记密码";

    @Autowired
    private EmailService emailService;

    @GetMapping("resetPwd")
    public CoreResult send(String email){
        if(StringUtils.isBlank(email)){
            return CoreResultBuilder.error("参数为空");
        }
        int randomCode = RandomUtils.nextInt(100000, 999999);
        String body = "您的验证码为："+randomCode;
        emailService.sendEmail(email,subject, body);

        // 发送成功后，将验证码保存起来
        ResetPwdCodeData.put(email,randomCode);
        return CoreResultBuilder.success();
    }

    @GetMapping("validCode")
    public CoreResult validCode(String email, Integer code){
        if(StringUtils.isBlank(email) || code==null){
            return CoreResultBuilder.error("参数为空");
        }
        return CoreResultBuilder.success(ResetPwdCodeData.valid(email,code));
    }
}
